package com.wtwd.campus.receiver;

import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.service.DeviceAppService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.IRMFeignClientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mjy
 * @date 2022/12/12
 * @description
 */
@Service
@MsgType(value = Constants.UPLOAD_APP)
public class UploadAppHandler implements IReceiverHandler {


    @Resource
    private IRMFeignClientService irmFeignClientService;

    @Resource
    private DeviceInfoService deviceInfoService;

    @Resource
    private DeviceAppService deviceAppService;


    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqUpLoadApp mqUpLoadApp = (MqUpLoadApp) mqMessage.getObject();
        String imei = mqUpLoadApp.getImei();
        String commandNumber = mqUpLoadApp.getCommandNumber();
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            List<MqAppInfo> appInfos = mqUpLoadApp.getAppInfos();
            if (appInfos != null && appInfos.size() > 0) {//进行app信息存储
                deviceAppService.dealUpLoadAppData(deviceInfo.getDeviceId(),mqUpLoadApp.getAppInfos(), mqUpLoadApp.getType());
            }
            MqGeneralReply mqGeneralReply = new MqGeneralReply();
            mqGeneralReply.setImei(imei);
            mqGeneralReply.setCommandNumber(commandNumber);
            //确认收到回复
            irmFeignClientService.replyUploadApp(mqGeneralReply);
        }
    }
}
